package com.comdosoft.comdoandroidbase.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.TextView;

import com.comdosoft.comdoandroidbase.R;

import java.util.ArrayList;
import java.util.List;


public class LeftSlideDeleteAdapter extends BaseAdapter{

	private List<String> dataList = new ArrayList<String>();
	private LayoutInflater inflater;
	private Context mContext;
	private ViewHolder viewHolder;
	private OnClickListener onClickListener;
	private int mScreentWidth; 

	private int selectPosition = -1;

	public int getSelectPosition() {
		return selectPosition;
	}

	public void setSelectPosition(int selectPosition) {
		this.selectPosition = selectPosition;
	}

	public LeftSlideDeleteAdapter(Context context, List<String> dataList,
			int screenWidth,OnClickListener onClickListener) {
		this.dataList = dataList;
		this.mContext = context;
		this.mScreentWidth = screenWidth;
		this.onClickListener = onClickListener;
		inflater = LayoutInflater.from(context);

	}

	@Override
	public int getCount() {
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {


		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_list_leftslide_delete, parent,
					false);
			viewHolder.textView = (TextView) convertView
					.findViewById(R.id.textView);

			viewHolder.itemHorizontalScrollView = (HorizontalScrollView) convertView.findViewById(R.id.itemHorizontalScrollView);  

			viewHolder.actionLayout = convertView.findViewById(R.id.ll_action);  
			viewHolder.delButton = (Button) convertView.findViewById(R.id.delButton);  
			viewHolder.delButton.setTag(position);  
			viewHolder.normalItemContentLayout = convertView.findViewById(R.id.ll_content);
			LayoutParams lp = viewHolder.normalItemContentLayout.getLayoutParams();  
			lp.width = mScreentWidth;  

			convertView.setTag(viewHolder);
		}else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.textView.setText(dataList.get(position));

		convertView.setOnTouchListener(new View.OnTouchListener(){  
			@Override  
			public boolean onTouch(View v, MotionEvent event)  
			{  
				switch (event.getAction())  
				{  
				case MotionEvent.ACTION_UP:  
					ViewHolder viewHolder = (ViewHolder) v.getTag();  
					int scrollX = viewHolder.itemHorizontalScrollView.getScrollX();  
					int actionLayoutWidth = viewHolder.actionLayout.getWidth();  
					//如果水平方向的移动值<操作区域的长度的一半,就复原  
					if (scrollX < actionLayoutWidth / 2)  
					{  
						viewHolder.itemHorizontalScrollView.smoothScrollTo(0, 0);  
					}  
					else//否则的话显示操作区域  
					{  
						viewHolder.itemHorizontalScrollView.smoothScrollTo(actionLayoutWidth , 0);  
					}  
					return true;  
				}  
				return false;  
			}  
		}); 

		if (viewHolder.itemHorizontalScrollView.getScrollX() != 0){  
			viewHolder.itemHorizontalScrollView.scrollTo(0, 0);  
		}

		viewHolder.delButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				onClickListener.onDelItem(position);
			}
		});
		viewHolder.normalItemContentLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				onClickListener.onNormalItem(position);
			}
		});
		return convertView;
	}

	static class ViewHolder {
		public TextView textView;

		public HorizontalScrollView itemHorizontalScrollView;
		public View normalItemContentLayout;  
		public View actionLayout;  
		public Button delButton;  
	}
	public interface OnClickListener {
		public void onNormalItem(int position);
		public void onDelItem(int position);
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}

}
